package com.wangtao.firstapp.adapters;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.activity.BaskDetailsActivity;
import com.wangtao.firstapp.activity.OriginalAuthorActivity;
import com.wangtao.firstapp.custom.MyGridView;
import com.wangtao.firstapp.modles.AllBatheData;

import de.hdodenhof.circleimageview.CircleImageView;

/**
 * 全部晒单页面 的适配器
 * Created by lanouhn on 16/9/11.
 */
public class AllBatheAdapter extends RecyclerView.Adapter<AllBatheAdapter.AllBatheViewHolder> {

    //用于存作者Id
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;


    private AllBPAdapter allBPAdapter;

    private Context context;
    private AllBatheData allBatheData;

    public AllBatheAdapter(Context context, AllBatheData allBatheData) {
        this.context = context;
        this.allBatheData = allBatheData;
    }

    @Override
    public AllBatheViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_shopdetails_bask, null);
        AllBatheViewHolder holder = new AllBatheViewHolder(view);

        sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        editor = sp.edit();
        return holder;
    }

    @Override
    public void onBindViewHolder(AllBatheViewHolder holder, final int position) {

        Picasso.with(context).load(allBatheData.getData().get(position).getUser().getAvatar()).into(holder.logo);
        holder.name.setText(allBatheData.getData().get(position).getUser().getNickname());
        holder.time.setText(allBatheData.getData().get(position).getDatestr());
        holder.content.setText(allBatheData.getData().get(position).getContent());
        //图片
//        allBPAdapter=new AllBPAdapter(context,allBatheData.getData().get(position).getPics());
//        holder.mygridview.setNumColumns(allBatheData.getData().get(position).getPics().size());
//        holder.mygridview.setAdapter(allBPAdapter);

        Picasso.with(context).load(allBatheData.getData().get(position).getPics().get(0).getUrl()).into(holder.shopPhone);

        //跳转至 晒单详情 页面
        holder.rltView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String baskdetails_ID=allBatheData.getData().get(position).getId();
                Intent intent=new Intent(context, BaskDetailsActivity.class);
                intent.putExtra("baskdetails_iD",baskdetails_ID);
                context.startActivity(intent);
            }
        });

        //跳转至，作者介绍页面
        holder.logo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String author_ID = allBatheData.getData().get(position).getUser().getUser_id();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                editor.putString("author_iD", author_ID);
                editor.commit();
                context.startActivity(intent);
            }
        });

        //跳转至，作者介绍页面
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String author_ID = allBatheData.getData().get(position).getUser().getUser_id();
                Intent intent = new Intent(context, OriginalAuthorActivity.class);
                editor.putString("author_iD", author_ID);
                editor.commit();
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return allBatheData.getData().size();
    }

    public class AllBatheViewHolder extends RecyclerView.ViewHolder {
        private ImageView logo;
        private TextView name, time, content;
        private LinearLayout lltView;
        private MyGridView mygridview;
        private ImageView shopPhone;

        private RelativeLayout rltView;

        public AllBatheViewHolder(View itemView) {
            super(itemView);

            rltView = (RelativeLayout) itemView.findViewById(R.id.rltView_item_shopdetails_bask);//下边的数据部分的容器，用于隐藏，显示
            logo = (CircleImageView) itemView.findViewById(R.id.logo_item_shopdetails_bask);//作者头像
            name = (TextView) itemView.findViewById(R.id.name_item_shopdetails_bask);//作者昵称
            time = (TextView) itemView.findViewById(R.id.time_item_shopdetails_bask);//晒单时间
            content = (TextView) itemView.findViewById(R.id.content_item_shopdetails_bask);//晒单内容，，文字
            //mygridview = (MyGridView) itemView.findViewById(R.id.mygridview_item_shopdetails_bask);//用于加载晒单的图片
            shopPhone= (ImageView) itemView.findViewById(R.id.shopPhoto_item_shopdetails_bask);//图片
        }
    }
}
